package com.jt.tenant.action;

import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/13.
 */
public class LocationModular {

    private List<OnLocateionChangeCallBack> list = new ArrayList<>();

    private AMapLocationClientOption mLocationOption;

    private AMapLocationClient aMapLocationClient;

    private AMapLocation aMapLocation;

    public LocationModular(Context context){

        mLocationOption = new AMapLocationClientOption();

        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);

        mLocationOption.setNeedAddress(true);

        mLocationOption.setOnceLocation(false);

        mLocationOption.setWifiActiveScan(true);

        mLocationOption.setMockEnable(false);

        mLocationOption.setInterval(2000);

        aMapLocationClient = new AMapLocationClient(context);

        aMapLocationClient.setLocationListener(new AMapLocationListener() {

            private boolean isOne, isTwo;

            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {

                if (aMapLocation != null && aMapLocation.getErrorCode() == 0) {

//                    Log.e("LocationModular", aMapLocation.getCity());

                    LocationModular.this.aMapLocation = aMapLocation;

                    if (!isOne) {

                        location();

                        isOne = true;

                        mLocationOption.setInterval(60000);

                        aMapLocationClient.stopLocation();

                        aMapLocationClient.startLocation();

                    }else if(isTwo){

                        locationChange();

                    }else{

                        isTwo = true;

                    }

                }

            }

        });

        aMapLocationClient.setLocationOption(mLocationOption);

        aMapLocationClient.startLocation();

    }

    public void addOnLocateionChangeCallBack(OnLocateionChangeCallBack onLocateionChangeCallBack){

        if(!list.contains(onLocateionChangeCallBack)){

            list.add(onLocateionChangeCallBack);

        }

        if(aMapLocation != null && onLocateionChangeCallBack.isAddChange){

            onLocateionChangeCallBack.onLocation(onLocateionChangeCallBack.aMapLocation = aMapLocation);

        }


    }

    public void removeOnLocateionChangeCallBack(OnLocateionChangeCallBack onLocateionChangeCallBack){

        list.remove(onLocateionChangeCallBack);

        aMapLocationClient.stopLocation();

    }

    public void location(){

        if(aMapLocation != null){

            for (int i = 0 ; i < list.size() ; i ++){

                OnLocateionChangeCallBack onLocateionChangeCallBack = list.get(i);

                onLocateionChangeCallBack.onLocation(onLocateionChangeCallBack.aMapLocation = aMapLocation);

            }

        }

    }

    private void locationChange(){

        if(aMapLocation != null){

            for (int i = 0 ; i < list.size() ; i ++){

                OnLocateionChangeCallBack onLocateionChangeCallBack = list.get(i);

                onLocateionChangeCallBack.onLocationChange(onLocateionChangeCallBack.aMapLocation = aMapLocation);

            }

        }

    }

    public static class OnLocateionChangeCallBack{

        public AMapLocation aMapLocation;

        public boolean isAddChange = true;

        public void onLocation(AMapLocation aMapLocation){}

        public void onLocationChange(AMapLocation aMapLocation){}

    }

}
